package com.etogc.groupchat.group.ui;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.etogc.groupchat.R;
import com.etogc.groupchat.base.BaseActivity;
import com.etogc.groupchat.bean.KFBean;
import com.etogc.groupchat.group.biz.GroupHttpBiz;
import com.etogc.http.ReqDataHandler;
import com.etogc.http.ReqErrorHandler;
import com.etogc.http.VolleyHttpUtil;
import com.etogc.util.GcUrl;
import com.etogc.util.YcToastHelper;
import com.etogc.view.MyOptionPicker;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.qqtheme.framework.picker.OptionPicker;

/**
 * 添加群组
 */
public class CreateGroupActivity extends BaseActivity implements View.OnClickListener {

    private TextView tvPay;
    private TextView tvContact;
    private EditText etGroupName;
    private EditText etRemark;
    private EditText etPeopleNum;
    private String lotteryType = "1";//1 时时彩 2赛车
    private MyOptionPicker mPicker;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_add);
        tvPay = findViewById(R.id.tvPay);
        etGroupName = findViewById(R.id.etGroupName);
        tvContact = findViewById(R.id.tvContact);
        etRemark = findViewById(R.id.etRemark);
        etPeopleNum = findViewById(R.id.etPeopleNum);
        RelativeLayout rlPay = findViewById(R.id.rlPay);
        Button btnCreate = findViewById(R.id.btnCreate);
        initView();
        rlPay.setOnClickListener(this);
        btnCreate.setOnClickListener(this);
        requestKF();
        requestGroupFee();
    }

    /**
     * 获取客服
     */
    private void requestKF() {
        VolleyHttpUtil.getInstance().getWithCallback(GcUrl.GET_KF, new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                try {
                    JsonElement parse = new JsonParser().parse(response.toString());
                    JsonObject asJsonObject = parse.getAsJsonObject();
                    KFBean kfBean = new Gson().fromJson(asJsonObject, KFBean.class);
                    if (kfBean.getResultCode() == 200) {
                        KFBean.KF kf = kfBean.getData().get(0);
                        tvContact.setText(kf.getPhone());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }


            }
        });
    }

    /**
     * 获取群费用
     */
    private void requestGroupFee() {
        VolleyHttpUtil.getInstance().getWithCallback(GcUrl.GROUP_FEE, new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                boolean isSuccess = ReqErrorHandler.getInstance().isSuccess(response);
                if (isSuccess) {
                    List<Object> list = ReqDataHandler.getList(response);
                    List<String> priceList = new ArrayList<>();
                    if (list != null && list.size() > 0) {
                        for (int i = 0; i < list.size(); i++) {
                            HashMap<String, String> map = (HashMap<String, String>) list.get(i);
                            priceList.add(String.valueOf(map.get("remark")));
                        }
                    }
                    String[] strings = priceList.toArray(new String[0]);
                    tvPay.setText(strings[0]);
                    mPicker = new MyOptionPicker(CreateGroupActivity.this, strings);
                }
            }
        });
    }

    private void initView() {
        setTitle("申请群");
        showBack();
    }

    public void showPay(View view) {
        mPicker.setOnOptionPickListener(new OptionPicker.OnOptionPickListener() {
            @Override
            public void onOptionPicked(int index, String item) {
                tvPay.setText(item);
            }
        });
        mPicker.show();
    }

    private void requestCreateGroup() {
        //校验输入参数
        //请求创建群组
        //请求成功失败
        String groupName = etGroupName.getText().toString();
        String remark = etRemark.getText().toString();
        if (groupName.equals("")) {
            YcToastHelper.getInstance()._toast("您的群名还未填写");
        } else if (remark.equals("")) {
            YcToastHelper.getInstance()._toast("您的备注还未填写");
        } else {
            String pay = tvPay.getText().toString();
            try {
                String s[] = pay.split("/");
                String count = s[1];
                count = count.substring(0, count.length() - 1);
                GroupHttpBiz.createGroup(this, groupName, remark, count);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ivBack:
                finish();
                break;
            case R.id.rlPay:
                showPay(view);
                break;
            case R.id.btnCreate:
                requestCreateGroup();
                break;
        }

    }
}
